package com.tsvetanv.system;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.util.Assert;
import org.springframework.web.servlet.handler.AbstractHandlerMapping;

public class RequestParameterHandlerMapping extends AbstractHandlerMapping {

	private String parameterName = "handler";
	private final Map<String, Object> paramMappings = new HashMap<String, Object>();

	@Override
	protected Object getHandlerInternal(HttpServletRequest request)
			throws Exception {
		String parameterValue = request.getParameter(parameterName);
		return paramMappings.get(parameterValue);
	}

	public void setParameterName(String parameterName) {
		this.parameterName = parameterName;
	}

	public final void setParamMappings(Map<String, Object> paramMappings) {
		this.paramMappings.putAll(paramMappings);
	}

	public void afterPropertiesSet() throws Exception {
		Assert.hasText(parameterName, "parameterName must not be null or blank");
	}

}
